package com.wyx.bbs.controller;

import com.wyx.bbs.domain.Article;
import com.wyx.bbs.domain.ArticleToUser;
import com.wyx.bbs.domain.Focus;
import com.wyx.bbs.service.FocusService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 关注
 * @Date: Created in 16:31 2021/4/26
 * @Modefied By:
 */
@Controller
@RequestMapping("/focus")
public class FocusController {

@Resource
FocusService focusService;


Map map = new HashMap();
private Object resultMap(int code, String msg){
    map.put("code",code);
    map.put("msg",msg);
    return map;
}

/**
 * 添加/取消关注
 * @param focus
 * @return
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Object addFocus(@RequestBody Focus focus){

    int focusd = focusService.addFocus(focus);
    if(focusd!=0){
        resultMap(200, "成功");

    }

    return map;
}

/**
 * 查询关注列表
 * @return
 */
@RequestMapping("/followList")
@ResponseBody
public Object focusList(@RequestParam Integer uid){
    List<ArticleToUser> followList = focusService.focusList(uid);

    List<ArticleToUser> result = new ArrayList<>();

    for(ArticleToUser follow : followList){
        if(follow.getIsFocus()!=0){
            result.add(follow);
        }
    }

    if(followList!=null){
        resultMap(200,"成功");
        map.put("list", result);
    }
    return map;
}

/**
 * 查询id是否被关注
 * @return
 */
@RequestMapping("/isFollow")
@ResponseBody
public Object isFollow(@RequestParam Integer uid, @RequestParam Integer id){

    map.put("code",200);
    map.put("msg","成功");

    if(uid==0){
        map.put("hasFollow",0);
        return map;
    }
    List<ArticleToUser> followList = focusService.isFollow(uid, id);

    int hasFollow = followList.get(0).getIsFocus();


    map.put("hasFollow",hasFollow);
    return map;
}


}
